// pages/tupian/tupian.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: "",
    id: 0,
    answer: ["cuo", "cuo", "cuo", "cuo"],//当前答案
    state: ["none", "none", "none", "none"],
    analyze: "none",
    laststate: "",
    touch: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let api = this.apichange(options.id);
    console.log(options.id)
    wx.request({
      url: `${app.globalData.apiurl}${api}/list`,
      method: "GET",
      data: {
        account: 10
      },
      success: res => {
        let count = res.data.length
        for (let i = 0; i < count; i++) {
          if(res.data[i].image==""){
            res.data[i].img="none"
            res.data[i].txt=""
          }else{
            res.data[i].img=""
            res.data[i].txt="none"
          }
          if (res.data[i].answer == "A") {
            res.data[i].answer = ["dui", "cuo", "cuo", "cuo"]
          }
          if (res.data[i].answer == "B") {
            res.data[i].answer = ["cuo", "dui", "cuo", "cuo"]
          }
          if (res.data[i].answer == "C") {
            res.data[i].answer = ["cuo", "cuo", "dui", "cuo"]
          }
          if (res.data[i].answer == "D") {
            res.data[i].answer = ["cuo", "cuo", "cuo", "dui"]
          }
        }
        this.setData({
          items: res.data,
          answer: res.data[this.data.id].answer
        })
        console.log(this.data.items[this.data.id])
      }
    })
  },
  //api转换
  apichange: function (id) {
    if (id == 4) {
      let name = "gqbSx1Option"
      return name;
    }
    if (id == 7) {
      let name = "gqbSx2Option"
      return name;
    }
    if(id == 13){
      let name = "gqbLhOption"
      return name;
    }
    if(id == 14){
      let name = "gqzSx1Option"
      return name;
    }
    if (id == 17) {
      let name = "gqzSx2Option"
      return name;
    }
    if (id == 31) {
      let name = "zsbSx1Option"
      return name;
    }
    if (id == 34) {
      let name = "zsbSx2Option"
      return name;
    }
  },

  //下一题点击事件
  bindNext: function (e) {
    let state = this.data.state;
    state = ["none", "none", "none", "none"];
    this.setData({
      state: state,
      analyze: "none",
      laststate: "",
      touch:0
    })
    if (this.data.id == 9) {
      wx.showToast({
        title: '已经做完10题！点左上角返回！',
        icon: "none"
      })
      return false;
    } else {
      let ide = this.data.id * 1 + 1
      console.log(ide)
      this.setData({
        id: ide,
        answer: this.data.items[ide].answer
      })
      console.log(this.data.items[this.data.id])
    }
  },
  //上一题点击事件
  bindLast: function (e) {
    let state = this.data.state;
    state = ["none", "none", "none", "none"];
    this.setData({
      state: state,
      analyze: "none",
    })
    if (this.data.id == 0) {
      wx.showToast({
        title: '无上一题！',
        icon: "none"
      })
      return false;
    } else {
      let ide = this.data.id * 1 - 1
      this.setData({
        id: ide,
        answer: this.data.items[ide].answer,
        touch: 0
      })
      if (ide == 0) {
        this.setData({
          laststate: ""
        })
      }
      console.log(this.data.items[this.data.id])
    }
  },
  //选项点击事件
  chance: function (e) {
    console.log(e.currentTarget.id)
    if (this.data.touch == 1) {
      return false;
    }
    if (this.data.answer[e.currentTarget.id] == "cuo") {
    if (this.data.state[e.currentTarget.id] == "none") {
      let state = this.data.state;
      state[e.currentTarget.id] = "";
      this.setData({
        state: state,
        analyze: ""
      })
    }
  }else{
      let state = this.data.state;
      state[e.currentTarget.id] = "";
      this.setData({
        state: state,
        touch: 1
      })
    }
  },
  //点击图预览
  pretitle:function(e){
    let id = this.data.id
    console.log(this.data.items[id])
    wx.previewImage({
      urls: [this.data.items[id].image],
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})